﻿using System;
using System.Data;
using System.Drawing;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class ViewStudentResult : System.Web.UI.Page
    {
        StudentResultManager manager = new StudentResultManager();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void studentSearchButton_Click(object sender, EventArgs e)
        {
            string selectedRegNo = regNoTextBox.Text;
            GetStudentInformation(selectedRegNo);
            SetCurrentTime();
            GetCourseResult(selectedRegNo);
        }

        private void GetCourseResult(string selectedRegNo)
        {

            try
            {
                noOfEnrollCourseTextBox.Text = manager.GetAllEnrollCourses(selectedRegNo);
                noPfCompletedCourseTextBox.Text = manager.GetAllCompletedCourse(selectedRegNo);
                noOfCourseRemainingTextBox.Text = manager.GetAllRemainingCourses(selectedRegNo);
                totalCreditEnrollCourseTextBox.Text = manager.TotalCreditForEnrollCourses(selectedRegNo);
                completedCreditTextBox.Text = manager.GetAllCompletedCredit(selectedRegNo);
                remainingCreditTextBox.Text = manager.GetAllRemainingCredit(selectedRegNo);
                cgpaTextBox.Text = manager.GetCurrentCgpa(selectedRegNo);
                gradeLetterTextBox.Text = manager.GetCurrentGradeLetter(selectedRegNo);
                certificateConfomationMessageLabel.ForeColor = Color.CadetBlue;
                certificateConfomationMessageLabel.Text = manager.CertificateInformation(selectedRegNo);
                DataTable courseTable = manager.GetComplitedCourses(selectedRegNo);
                courseGridView.DataSource = courseTable;
                courseGridView.DataBind();
            }
            catch (Exception exception)
            {

                certificateConfomationMessageLabel.ForeColor = Color.Red;
                certificateConfomationMessageLabel.Text = exception.Message;
            }
            
        }

        private void SetCurrentTime()
        {
            resultAsOfLabel.Text = "Result as of:";
            DateTime time = DateTime.Now;
            dateLabel.ForeColor = Color.BlueViolet;
            dateLabel.Text = time.Date.ToString("d");
            timeLabel.ForeColor = Color.BlueViolet;
            timeLabel.Text = time.ToString("t");
        }

        private void GetStudentInformation(string selectedRegNo)
        {
            selectedRegNo = regNoTextBox.Text;
            Student aStudent = manager.SearchStudentResultByRegNo(selectedRegNo);
            studentNameTextBox.Text = aStudent.Name;
            emailTextBox.Text = aStudent.Email;
            departmentTextBox.Text = aStudent.Department.Name;
            departmentIdLabel.Text = aStudent.Department.DeptId.ToString();
        }
    }
}